package cn.xyy.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xyy.system.dao.DictDao;
import cn.xyy.system.dao.DictItemDao;
import cn.xyy.web.AuthUserHolder;
import cn.xyy.common.domain.system.auth.Dict;
import cn.xyy.common.domain.system.auth.DictItem;
import cn.xyy.common.domain.system.auth.DictItemVO;
import cn.xyy.common.domain.system.auth.DictVO;
import cn.xyy.common.utils.BeanUtils;
import cn.xyy.common.utils.DateUtils;
import cn.xyy.common.utils.StringUtils;
import cn.xyy.common.exception.SystemException;
import cn.xyy.common.domain.mvc.Page;

/**
 * 字典业务层
 * 
 * @author xyy
 *
 */
@Service
public class DictService {

	@Autowired
	private DictDao dictDao;

	@Autowired
	private DictItemDao dictItemDao;

	public List<Dict> getDictAllList() {
		return dictDao.select(new Dict());
	}

	public void addDict(Dict dict) {
		dict.setCreator(AuthUserHolder.getUserInfo().getUsername());
		dict.setCreateTime(DateUtils.getDateTime());
		int result = dictDao.insert(dict);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	public void updateDict(DictVO vo) {

		Dict dictInfo = dictDao.selectById(vo.getId(), Dict.class);
		if (dictInfo == null) {
			throw new SystemException("未查询到字典信息");
		}
		BeanUtils.copyProperties(vo, dictInfo);
		dictInfo.setUpdater(AuthUserHolder.getUserInfo().getUsername());
		dictInfo.setUpdateTime(DateUtils.getDateTime());

		int result = dictDao.update(dictInfo);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	public void deleteDict(String id) {
		if (id == null) {
			throw new SystemException("删除条件不能为空");
		}
		int result = dictDao.delete(new Dict().setId(id));
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	// -----------------------------------------

	public List<DictItem> getDictItemList(String dictName) {
		if (StringUtils.isBlank(dictName)) {
			throw new SystemException("字典名称不能为空");
		}
		return dictItemDao.select(dictName);
	}

	public Page<DictItem> getDictItemPage(DictItemVO vo) {
		return dictItemDao.selectPage(vo);
	}

	public void addDictItem(DictItem item) {
		item.setCreator(AuthUserHolder.getUserInfo().getUsername());
		item.setCreateTime(DateUtils.getDateTime());
		int result = dictItemDao.insert(item);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	public void updateDictItem(DictItemVO vo) {
		DictItem dictInfo = dictItemDao.selectById(vo.getId(), DictItem.class);
		if (dictInfo == null) {
			throw new SystemException("未查询到字典信息");
		}
		BeanUtils.copyProperties(vo, dictInfo);
		dictInfo.setUpdater(AuthUserHolder.getUserInfo().getUsername());
		dictInfo.setUpdateTime(DateUtils.getDateTime());

		int result = dictDao.update(dictInfo);
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

	public void deleteDictItem(String id) {
		if (id == null) {
			throw new SystemException("删除条件不能为空");
		}
		int result = dictItemDao.delete(new DictItem().setId(id));
		if (result < 0) {
			throw new SystemException("执行失败");
		}
	}

}
